package com.hezhi.project.module.message.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hezhi.common.utils.StringUtils;
import com.hezhi.common.utils.security.ShiroUtils;
import com.hezhi.project.module.classes.domain.Classes;
import com.hezhi.project.module.classes.mapper.ClassesMapper;
import com.hezhi.project.module.grade.domain.Grade;
import com.hezhi.project.module.grade.mapper.GradeMapper;
import com.hezhi.project.module.message.domain.MessageDomain;
import com.hezhi.project.module.message.mapper.MessageMapper;
import com.hezhi.project.module.parent.domain.Parent;
import com.hezhi.project.module.parent.mapper.ParentMapper;
import com.hezhi.project.module.recipient.domain.Recipient;
import com.hezhi.project.module.recipient.mapper.RecipientMapper;

@Service
public class MessageServiceImpl implements IMessageService{
	 @Autowired
	    private MessageMapper noticeMapper;
	    @Autowired
	    private GradeMapper gradeMapper;
	    @Autowired
	    private ClassesMapper classesMapper;
	    @Autowired
	    private ParentMapper parentMapper;
	    @Autowired
	    private RecipientMapper recipientMapper;

	    /**
	     * 查询公告信息
	     * 
	     * @param noticeId 公告ID
	     * @return 公告信息
	     */
	    @Override
	    public MessageDomain selectNoticeById(Integer noticeId)
	    {
	        return noticeMapper.selectNoticeById(noticeId);
	    }

	    /**
	     * 查询公告列表
	     * 
	     * @param notice 公告信息
	     * @return 公告集合
	     */
	    @Override
	    public List<MessageDomain> selectNoticeList(MessageDomain notice)
	    {
	        return noticeMapper.selectNoticeList(notice);
	    }

	    /**
	     * 新增公告
	     * 
	     * @param notice 公告信息
	     * @return 结果
	     */
	    @Override
	    public int insertNotice(MessageDomain notice)
	    {
	        return noticeMapper.insertNotice(notice);
	    }

	    /**
	     * 修改公告
	     * 
	     * @param notice 公告信息
	     * @return 结果
	     */
	    @Override
	    public int updateNotice(MessageDomain notice)
	    {
	        return noticeMapper.updateNotice(notice);
	    }

	    /**
	     * 保存公告
	     * 
	     * @param notice 公告信息
	     * @return 结果
	     */
	    @Override
	    @Transactional
	    public int saveNotice(MessageDomain notice)
	    {
	        Integer noticeId = notice.getId();
	        int rows = 0;
	        if (StringUtils.isNotNull(noticeId))
	        {
	            notice.setUpdateBy(ShiroUtils.getLoginName());
	            // 修改公告
	            rows = noticeMapper.updateNotice(notice);
	        }
	        else
	        {
	            notice.setCreateBy(ShiroUtils.getLoginName());
	            // 新增公告
	            rows = noticeMapper.insertNotice(notice);
	        }
	        String[] str = notice.getSelectids().split("\\|");
        	recipientMapper.insertRecipientAll(notice.getId().toString(),str[0],str[1],str[2],str[3]);
	        return rows;
	    }
	    

//	    /**
//	     * 删除公告对象
//	     * 
//	     * @param ids 需要删除的数据ID
//	     * @return 结果
//	     */
//	    @Override
//	    public int deleteNoticeByIds(String ids)
//	    {
//	        return noticeMapper.deleteNoticeByIds(Convert.toIntArray(ids));
//	    }

		@Override
		public List<Map<String, Object>> findSelectList() {
			List<Map<String, Object>> list = new ArrayList<>();
			//定义角色
			Map<String, Object> s1 = new HashMap<>();
			s1.put("id", "R1");
			s1.put("pid", "根目录");
			s1.put("name", "家长");
			list.add(s1);
			//定义年级
			List<Grade> listGrade = gradeMapper.selectGradeList(new Grade());
			for(Grade g:listGrade){
				Map<String, Object> s = new HashMap<>();
				s.put("id", g.getGradeNumber());
				s.put("pid", "R1");
				s.put("name", g.getName());
				list.add(s);
			}
			//定义班级
			List<Classes> listClasses = classesMapper.selectClassesList(new Classes());
			for(Classes c:listClasses){
				Map<String, Object> s = new HashMap<>();
				s.put("id", c.getClassNumber());
				s.put("pid", c.getGradeNumber());
				s.put("name", c.getName());
				list.add(s);
			}
			//定义人员
			List<Parent> listParent = parentMapper.selectParentListToClasses();
			for(Parent p:listParent){
				Map<String, Object> s = new HashMap<>();
				s.put("id", p.getParentId());
				s.put("pid", p.getStudentNumber());
				s.put("name", p.getName());
				list.add(s);
			}
			return list;
		}

		/**
		 * 根据接收表查询通知
		 */
		@Override
		public List<MessageDomain> selectNoticeListByRecipient(Recipient recipient) {
			return noticeMapper.selectNoticeListByRecipient(recipient);
		}

		@Override
		public int deleteNoticeByIds(Integer[] noticeIds) {
			// TODO Auto-generated method stub
			return 0;
		}
}
